/* Includes ------------------------------------------------------------------*/

#include "stm32f10x.h"
#include "protocols.h"
#include "usb_desc.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

uint8_t selected_protocol=0;

extern uint8_t value_buffer_0[USB_DATA_SIZE];
extern uint8_t value_buffer_1[USB_DATA_SIZE];
extern uint16_t value_buffer_fill[3];

/* Private functions ---------------------------------------------------------*/



/* Public  functions ---------------------------------------------------------*/

void define_protocol(void)
{	

	if (selected_protocol == 0)
	{
		/* define the protocol header */
		value_buffer_0[0] =  0xFE;
		value_buffer_0[1] =  0xAF;	/* identifier */
		
		value_buffer_0[2] =  0xC0;
		value_buffer_0[3] =  0xC0;	/* identifier */
		
		value_buffer_0[4] =  0x34;
		value_buffer_0[5] =  0x12;	/* identifier */
		
		value_buffer_0[6] = 0x45;
		value_buffer_0[7] = 0x23;	/* identifier */
		
		value_buffer_0[8] = 0x10;
		value_buffer_0[9] = 0x00;	/* header size */
	
		
		/* define the top values used in the protocol */
		value_buffer_0[12] =  0x01;
		value_buffer_0[13] =  0x00; /* constant */
				
		value_buffer_0[14] =  0x00;
		value_buffer_0[15] =  0x00; /* dummy word */
		
		value_buffer_0[16] =  0x00;
		value_buffer_0[17] =  0x00; /* dummy word */
		
		value_buffer_0[18] = 0x00;
		value_buffer_0[19] = 0x00; /* dummy word */
		
		value_buffer_0[20] = 0x00;
		value_buffer_0[21] = 0x00; /* dummy word */
		
		value_buffer_0[22] = 0x00;
		value_buffer_0[23] = 0x00; /* dummy word */
		
		value_buffer_0[24] = 0x00;
		value_buffer_0[25] = 0x00; /* dummy word */
		
		value_buffer_0[26] = 0x00;
		value_buffer_0[27] = 0x00; /* dummy word */
		
		value_buffer_0[28] = 0x00;
		value_buffer_0[29] = 0x00; /* dummy word */
	
	
	
		/* define the protocol header */
		value_buffer_1[0] =  0xFE;
		value_buffer_1[1] =  0xAF;	/* identifier */
		
		value_buffer_1[2] =  0xC0;
		value_buffer_1[3] =  0xC0;	/* identifier */
		
		value_buffer_1[4] =  0x34;
		value_buffer_1[5] =  0x12;	/* identifier */
		
		value_buffer_1[6] = 0x45;
		value_buffer_1[7] = 0x23;	/* identifier */
		
		value_buffer_1[8] = 0x10;
		value_buffer_1[9] = 0x00;	/* header size */
	
		
		/* define the top values used in the protocol */
		value_buffer_1[12] =  0x01;
		value_buffer_1[13] =  0x00; /* constant */
				
		value_buffer_1[14] =  0x00;
		value_buffer_1[15] =  0x00; /* dummy word */
		
		value_buffer_1[16] =  0x00;
		value_buffer_1[17] =  0x00; /* dummy word */
		
		value_buffer_1[18] = 0x00;
		value_buffer_1[19] = 0x00; /* dummy word */
		
		value_buffer_1[20] = 0x00;
		value_buffer_1[21] = 0x00; /* dummy word */
		
		value_buffer_1[22] = 0x00;
		value_buffer_1[23] = 0x00; /* dummy word */
		
		value_buffer_1[24] = 0x00;
		value_buffer_1[25] = 0x00; /* dummy word */
		
		value_buffer_1[26] = 0x00;
		value_buffer_1[27] = 0x00; /* dummy word */
		
		value_buffer_1[28] = 0x00;
		value_buffer_1[29] = 0x00; /* dummy word */

		value_buffer_fill[0]=0;
		value_buffer_fill[1]=32;
		value_buffer_fill[2]=32;
	}
	else
	{
		value_buffer_fill[0]=0;
		value_buffer_fill[1]=0;
		value_buffer_fill[2]=0;
	}

}


uint16_t get_checksum(uint8_t *data, uint8_t block_size)
{

	uint16_t checksum=0;	
	uint32_t dummy_sum = 0xA647;

	while(block_size > 10)
	{
		if((block_size-1) % 2 == 0)
			dummy_sum += *(data+block_size-1);
		else
			dummy_sum += *(data+block_size-1)*0x100;
		block_size--;
	}
	
	checksum= dummy_sum & 0x7FFF;	

	return checksum;

}
